﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _70_7001_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            if (Request.QueryString["mode"] == "new")
            {
                this.lab_title.Text = dao.Get_titleName(7001) + " - 新增";
                this.lab_msg0.Visible = false;
                this.lab_msg1.Visible = false;
            }
            else
            {
                this.lab_title.Text = dao.Get_titleName(7001) + " - 修改";
                this.hidd_no.Value = Request.QueryString["ID"];

                people data = new peopleDAO().Get_people(int.Parse(this.hidd_no.Value));
                
                this.tbox_name.Text = data.peo_name;
                this.tbox_login.Text = data.peo_login;
                this.hidd_login.Value = data.peo_login;
                this.hidd_pw.Value = data.peo_passwd;

            }


        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        this.tbox_name.Focus();
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (this.Check_Input())
        {
            peopleDAO dao = new peopleDAO();
            people data;
            string str = "";
            if (this.hidd_no.Value != "")
            {
                data = dao.Get_people(int.Parse(this.hidd_no.Value));
                str = "修改完成!";

                if (this.hidd_login.Value != this.tbox_login.Text)
                {
                    data.peo_login = this.tbox_login.Text;
                }

                if (this.tbox_pw.Text.Length > 0)
                {
                    data.peo_passwd = this.tbox_pw.Text;
                }

            }
            else
            {
                data = new people();
                
                str = "新增完成!";

                data.peo_login = this.tbox_login.Text;
                data.peo_passwd = this.tbox_pw.Text;

                data.state = "1";
            }

            data.peo_name = this.tbox_name.Text;
            data.createtime = DateTime.Now;
            data.createuid = new SessionObject().SessionUserID;

            if (this.hidd_no.Value == "")
            {
                dao.Add_people(data);
            }

            dao.Update();

            JsUtil.UpdateParentJs(this, str);

        }
    }

    private bool Check_Input()
    {
        peopleDAO dao = new peopleDAO();

        if (string.IsNullOrEmpty(this.tbox_name.Text))
        {
            JsUtil.AlertJs(this, "請輸入名稱!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_login.Text))
        {
            JsUtil.AlertJs(this, "請輸入登入帳號!");
            return false;
        }
        else
        {
            if (this.tbox_login.Text.Length < 4)
            {
                JsUtil.AlertJs(this, "登入帳號需4~20個字元!");
                return false;
            }

            
            
        }

        

        //新建 帳號 密碼 檢查
        if (this.hidd_no.Value == "")
        {
            if (dao.Get_Check(this.tbox_login.Text) > 0)
            {
                JsUtil.AlertJs(this, "登入帳號重覆!");
                return false;
            }

            if (string.IsNullOrEmpty(this.tbox_pw.Text))
            {
                JsUtil.AlertJs(this, "請輸入新密碼!");
                return false;
            }
            else
            {
                if (this.tbox_pw.Text.Length < 4)
                {
                    JsUtil.AlertJs(this, "新密碼需4~20個字元!");
                    return false;
                }
            }

            if (string.IsNullOrEmpty(this.tbox_pw2.Text))
            {
                JsUtil.AlertJs(this, "請輸入確認新密碼!");
                return false;
            }

            if (this.tbox_pw.Text != this.tbox_pw2.Text)
            {
                JsUtil.AlertJs(this, "新密碼與確認新密碼不一致!");
                return false;
            }


        }
        else
        {
            //修改帳號
            if (this.hidd_login.Value != this.tbox_login.Text && dao.Get_Check_NotMe(this.tbox_login.Text, int.Parse(this.hidd_no.Value)) > 0)
            {
                JsUtil.AlertJs(this, "登入帳號重覆!");
                return false;
            }

            //修改密碼
            if (this.tbox_pw.Text.Length > 0)
            {
                if (this.tbox_pw.Text.Length < 4)
                {
                    JsUtil.AlertJs(this, "新密碼需4~20個字元!");
                    return false;
                }

                if (this.tbox_pw.Text != this.tbox_pw2.Text)
                {
                    JsUtil.AlertJs(this, "新密碼與確認新密碼不一致!");
                    return false;
                }

            }
        }

        
        
        


        return true;
    }
}